#
# Hill, Seth J. and Chris Tausanovitch. "A Disconnect in Representation? Comparison of Trends in Congressional and Public Polarization."
#


normalizeMultinomial <- function(output,n,m,K){
  bmin <- min(grep("beta",colnames(output)))
  bmax <- max(grep("beta",colnames(output)))
  amin <- min(grep("alpha",colnames(output)))
  amax <- max(grep("alpha",colnames(output)))
  xmin <- min(grep("x",colnames(output)))
  xmax <- max(grep("x",colnames(output)))

  x <- output[,xmin:xmax]
  alpha <- output[,amin:amax]
  beta <- output[,bmin:bmax]
  
  the999s <- which(alpha==999)

  for (i in 1:dim(output)[1]){
    sdx <- sd(x[i,])
    meanx <- mean(x[i,])
    x[i,] <- (x[i,] - meanx)/sdx
    alpha[i,] <- alpha[i,] - beta[i,]*meanx
    beta[i,] <- beta[i,]*sdx
  }
  alpha[the999s] <- NA
  beta[the999s] <- NA
  return(list(x=x,beta=beta,alpha=alpha))
}

load("jagsoutJoint2k.RData")

output <- normalizeMultinomial(output[[1]],jdata$n,jdata$m,jdata$K)

polaritybeta <- 11
#setting the polarity
polarity <- ifelse(colMeans(output$beta)[polaritybeta] > 0 ,1,-1)
output$beta <- output$beta*polarity
output$x <- output$x*polarity

years <- c(1984, 1986, 1988, 1990, 1992, 1994, 1996, 2000, 2004, 2008)
loc <- "/home/chris/Documents/projects/seth/"
runtype="long"

library(foreign)

data <- read.dta("anes_timeseries_cdf.dta")
#read.dta("anes_cdf.dta")

anesyears  <- data$VCF0004
aneswgt <- data$VCF0009
anesparty <- rep(2,length(data$VCF0301))
anesparty[data$VCF0301 %in% c("2. Weak Democrat","1. Strong Democrat")] <- 1
anesparty[data$VCF0301 %in% c("6. Weak Republican","7. Strong Republican")] <- 3

prezvote <- rep(NA,dim(data)[1])
housevote <- rep(NA,dim(data)[1])
senatevote <- rep(NA,dim(data)[1])

prezvote[data$VCF0704=="2. Republican"] <- 0
prezvote[data$VCF0704=="1. Democrat"] <- 1
housevote[data$VCF0707=="2. Republican"] <- 0
housevote[data$VCF0707=="1. Democrat"] <- 1
senatevote[data$VCF0708=="2. Republican"] <- 0
senatevote[data$VCF0708=="1. Democrat"] <- 1

rm(data)

datayears <- anesyears[anesyears %in% years]
datawgt <- aneswgt[anesyears %in% years]

sds <- matrix(NA,3,length(years))
for (year in years){
    sds[,years==year] <- quantile(apply(output$x[,datayears==year],1,sd),c(.025,.5,.975))
}
pdf(paste("sd_long_multinomial.pdf",sep=""),width=8,height=6)
plot(sds[2,]~years,axes=F,ylim=c(.9,1.1),ylab="SD(ideal points)",xlab="year")
segments(years,sds[1,],years,sds[3,])
axis(1,labels=years,at=years)
axis(2)
abline(h=1,col="grey")
dev.off()
